<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { onMounted } from 'vue'
import { useAuthStore } from './stores/auth'

const authStore = useAuthStore()

onMounted(() => {
  authStore.initAuth()
})
</script>

<template>
  <header>
    <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <div class="app-title">
        <h1>易经占卜应用</h1>
        <p v-if="authStore.isLoggedIn" class="user-info">
          欢迎，{{ authStore.currentUser?.username }}！
        </p>
      </div>

      <nav>
        <RouterLink to="/">首页</RouterLink>
        <RouterLink to="/divination">算卦</RouterLink>
        <RouterLink to="/hexagrams">六十四卦</RouterLink>
        <RouterLink to="/about">关于</RouterLink>
        
        <div class="auth-links">
          <template v-if="authStore.isLoggedIn">
            <button @click="authStore.logout" class="logout-btn">登出</button>
          </template>
          <template v-else>
            <RouterLink to="/login">登录</RouterLink>
            <RouterLink to="/register">注册</RouterLink>
          </template>
        </div>
      </nav>
    </div>
  </header>

  <RouterView />
</template>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 2rem;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

.app-title h1 {
  margin: 0;
  font-size: 2rem;
  font-weight: 600;
}

.user-info {
  margin: 0.5rem 0 0 0;
  font-size: 1rem;
  opacity: 0.9;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}

nav a.router-link-exact-active {
  color: #ffd700;
  font-weight: 600;
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0.5rem 1rem;
  border-left: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  text-decoration: none;
  transition: color 0.3s ease;
}

nav a:hover {
  color: #ffd700;
}

nav a:first-of-type {
  border: 0;
}

.auth-links {
  display: flex;
  gap: 1rem;
}

.logout-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  color: #ffd700;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
    flex-direction: column;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
    width: 100%;
  }
}
</style>
